<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://demo.thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\common\facade\Arr;
use app\wechat\service\WechatService;
use GuzzleHttp\Client;
use library\Controller;
use library\File;
use think\Db;

/**
 * 推广人员
 * Class Index
 * @package app\admin\controller
 */
class ExtensionUser extends Controller
{
     protected $table = 'ExtensionUser';

    /**
     * 推广人员
     * @auth true
     * @menu true
     */
    public function index()
    {
        $this->title = '推广人员';

        $query = $this->_query($this->table)->like('name')->equal('id,name');
        $query->order('id desc')->page();
    }

    /**
     * 列表数据处理
     * @param array $data
     * @throws \Exception
     */
    protected function _index_page_filter(&$data)
    {

        foreach ($data as &$vo) {
            $vo['group_name'] = DB::name("Extension")
                                ->where('id',$vo['eid'])
                                ->value("name");
            //今日推广
            $start=strtotime(date("Y-m-d")." 00:00:00");
            $end=strtotime(date("Y-m-d"). " 23:59:59");                    
            $vo['today_num']=DB::name("extension_list")
                            ->where('created_at','between time',[$start, $end])
                            ->where('invitation_id',$vo['id'])
                            ->count();
            //推广总量
            $vo['total_num']=DB::name("extension_list")
                            ->where('invitation_id',$vo['id'])
                            ->count();
            //认证通过
            $vo['pass_num']=DB::name('extension_list')
                        ->alias('el')
                        ->join("user u",'u.wechat_openid=el.openid')
                        ->where('u.status',2)
                        ->where('el.invitation_id',$vo['id'])
                        ->count();
            //未认证
            $vo['no_num']=DB::name('extension_list')
                        ->alias('el')
                        ->join("user u",'u.wechat_openid=el.openid')
                        ->where('u.status',0)
                        ->where('el.invitation_id',$vo['id'])
                        ->count();
            //待认证
            $vo['pre_num']=DB::name('extension_list')
                        ->alias('el')
                        ->join("user u",'u.wechat_openid=el.openid')
                        ->where('u.status',1)
                        ->where('el.invitation_id',$vo['id'])
                        ->count();
            //拒绝
            $vo['deny_num']=DB::name('extension_list')
                        ->alias('el')
                        ->join("user u",'u.wechat_openid=el.openid')
                        ->where('u.status',3)
                        ->where('el.invitation_id',$vo['id'])
                        ->count();
           
        }
    }

    /**
     * 添加人员
     * @auth true
     */
    public function extensionUserAdd()
    {   

        $this->title = '添加人员';

        $cates=DB::name("Extension")->field("id,name")->select();
        $this->assign('cates',$cates);

        $this->_form($this->table, 'extension_user_add');
       
    }

    /**
     * 表单结果处理
     * @param boolean $result
     */
    protected function _extensionUserAdd_form_result($result)
    {
       $this->code($result);
    }
    /**
     * 编辑人员
     * @auth true
     */
    public function edit()
    {   

        $this->title = '编辑分组';
        $cates=DB::name("Extension")->field("id,name")->select();
        $this->assign('cates',$cates);

        $this->_form($this->table, 'extension_user_add');
       
    }


    /**
     * 获取推广码
     * @param $id
     *
     * @throws \WeChat\Exceptions\InvalidResponseException
     * @throws \WeChat\Exceptions\LocalCacheException
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function code($id)
    {
        //根据推广人员ID作为scene_id，唯一标识
        $ticketArr = WechatService::WeChatQrcode()
            ->create($id);
        $ticketGetUrl = WechatService::WeChatQrcode()->url(Arr::get($ticketArr, 'ticket'));

        $response = (new Client())->get($ticketGetUrl);
        if ($response->getStatusCode() == 200) {
            if ($response->getBody()->getSize()) {
                $result = $response->getBody()->getContents();


                $qrCodeFileName = 'qrcode/promote/' . $id . '.png';


                File::instance('local')->save($qrCodeFileName, $result);

                //更新二维码到推广表
                $data['code']=$qrCodeFileName;
                DB::name("ExtensionUser")->where('id',$id)->update($data);
            }
        }

    }


    /**
     * 查看推广码
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function qrcode(){

        $this->title="查看推广码";
        $id = $this->request->param('id', 0);
        $result=DB::name($this->table)
            ->alias('eu')
            ->join("Extension e",'eu.eid=e.id')
            ->field("eu.*,e.name as group_name")
            ->where('eu.id',$id)->find();
        $this->assign('result',$result);

        $this->fetch();
    }


    /**
     * 推广列表
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function extensionList(){

        $this->title = '推广列表';

        $query = $this->_query("extension_list")
                ->alias('el')
                ->join("ExtensionUser eu",'el.invitation_id=eu.id')
                ->join("Extension e",'e.id=eu.eid')
                ->join("User u",'u.wechat_openid=el.openid')
                ->field("el.*,eu.name as father_name,e.name as grand_name,u.wechat_nickname,u.type,u.status")
                ->like('el.openid')
                ->equal('el.openid');
        $query->order('id desc')->page();
    }


   



}
